package com.learn.egmvc3.controller;

import com.learn.egmvc3.mapper.NoticeMapper;
import com.learn.egmvc3.pojo.dto.NoticeAddParam;
import com.learn.egmvc3.pojo.dto.NoticeListQuery;
import com.learn.egmvc3.pojo.entity.Notice;
import com.learn.egmvc3.pojo.vo.NoticeDetailInfoVO;
import com.learn.egmvc3.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String addNotice(NoticeAddParam noticeAddParam){
        System.out.println("noticeAddParam:"+noticeAddParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int rs = noticeMapper.insertNotice(notice);
        System.out.println(rs>0?"新增公告成功":"新增公告失败");
        return "新增公告成功";
    }

    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> selectListNotice(NoticeListQuery noticeListQuery){
        System.out.println("noticeListQuery:"+noticeListQuery);

        List<NoticeListVO> noticeListVOS = noticeMapper.selectListNotice(noticeListQuery);
        return noticeListVOS;
    }

    @GetMapping("/v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfoVO selectNoticeDetailInfo(Long id){
        System.out.println("id:"+id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectNoticeDetail(id);
        return noticeDetailInfoVO;
    }
}
